<template>
  <van-popup v-model="show" :style="{ borderRadius: '8px' }">
    <div class="wrapper">
      <div class="title">选择标签</div>
      <div class="label-wrap">
        <div class="label" v-for="(item, index) in labelList" :key="index" @click="selected = index"
          :class="{ active: index === selected }">{{ item.tableName }}</div>
      </div>
      <div class="btn-group">
        <div class="btn btn-active" @click="show = false">取消</div>
        <div class="btn btn-active" @click="onConfirm">确定</div>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { getLabelList } from "@/api/appraisalProcess";
import { Toast } from "vant";
export default {
  props: {
    showPopup: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    showPopup(val) {
      this.show = val;
    },
    show(val) {
      if (!val) {
        this.$emit("update:showPopup", false);
      }
    }
  },
  created() {
    this.getLabelList();
  },
  data() {
    return {
      show: false,
      labelList: [],
      selected: ""
    };
  },
  methods: {
    getLabelList() {
      getLabelList(this.$route.query.did).then(res => {
        this.labelList = res.data;
      });
    },
    onConfirm() {
      if (!this.selected && this.selected !== 0) {
        Toast("请选择标签");
        return;
      }
      this.$emit("success", this.labelList[this.selected]);
      this.show = false;
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  padding: 25px;
  width: 248px;
  box-sizing: border-box;

  .label-wrap {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    align-items: center;
    padding: 0 7px;
    margin-right: -22px;

    .label {
      width: 84px;
      line-height: 26px;
      border-radius: 4px;
      margin-top: 16px;
      margin-right: 16px;
      color: #bbc0c7;
      text-align: center;
      border: 1px solid transparent;
      background: rgba(204, 204, 204, 0.2);

      &.active {
        color: #2492ff;
        background: rgba(56, 127, 241, 0.1);
        border: 1px solid #2492ff;
      }
    }
  }

  .title {
    text-align: center;
    color: #000;
    font-size: 16px;
    line-height: 22px;
  }

  .btn-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 30px;

    .btn {
      width: 92px;
      line-height: 32px;
      color: #fff;
      background: #2492ff;
      border-radius: 6px;
      text-align: center;

      &:first-child {
        background: #f5f5f5;
        color: #999999;
      }
    }
  }
}</style>
